using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Spire.Presentation.Charts;

namespace Spire.Presentation.Demo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnRun_Click(object sender, EventArgs e)
        {
            Presentation presentation = new Presentation();

            //set background Image
            string ImageFile = @"..\..\..\..\..\..\Data\bg.png";
            RectangleF rect2 = new RectangleF(0, 0, presentation.SlideSize.Size.Width, presentation.SlideSize.Size.Height);
            presentation.Slides[0].Shapes.AppendEmbedImage(ShapeType.Rectangle, ImageFile, rect2);
            presentation.Slides[0].Shapes[0].Line.FillFormat.SolidFillColor.Color = Color.FloralWhite;

            //insert chart
            RectangleF rect = new RectangleF(50, 100, 400, 400);
            IChart chart = presentation.Slides[0].Shapes.AppendChart(Spire.Presentation.Charts.ChartType.Cylinder3DClustered, rect);

            //add chart Title
            chart.ChartTitle.TextProperties.Text = "Report";
            chart.ChartTitle.TextProperties.IsCentered = true;
            chart.ChartTitle.Height = 30;
            chart.HasTitle = true;

            //load data from XML file to datatable
            DataTable dataTable = LoadData();

            //load data from datatable to chart
            InitChartData(chart, dataTable);

            //save the document
            presentation.SaveToFile("chart.pptx", FileFormat.Pptx2010);

            System.Diagnostics.Process.Start("chart.pptx");

        }

        //function to load data from XML file to DataTable
        private DataTable LoadData()
        {
            DataSet ds = new DataSet();
            ds.ReadXmlSchema(@"..\..\..\..\..\..\Data\data-schema.xml");
            ds.ReadXml(@"..\..\..\..\..\..\Data\data.xml");

            return ds.Tables[0];
        }

        //function to load data from DataTable to IChart
        private void InitChartData(IChart chart, DataTable dataTable)
        {
            for (int c = 0; c < dataTable.Columns.Count; c++)
            {
                chart.ChartData[0, c].Text = dataTable.Columns[c].Caption;
            }

            for (int r = 0; r < dataTable.Rows.Count; r++)
            {
                object[] data = dataTable.Rows[r].ItemArray;
                for (int c = 0; c < data.Length; c++)
                {
                    chart.ChartData[r + 1, c].Value = data[c];
                }
            }
        }
    }
}